package com.wufun.overseas.main.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.wufun.baselibrary.download.CommonDownloadTask;
import com.wufun.baselibrary.impl.AdapterInterface;
import com.wufun.overseas.R;
import com.wufun.overseas.adapters.PlayMainItemAdapter;
import com.wufun.overseas.main.base.MyActivity;
import com.wufun.overseas.models.db.DownLoadModel;
import com.wufun.overseas.tools.DownloadManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by tangbl on 2017/12/10 0010.
 */

public class PlaySearchActivity extends MyActivity implements AdapterInterface {

    @Bind(R.id.edit_search_detail)
    EditText editSearchDetail;
    @Bind(R.id.playmain_listview)
    ListView playmainListview;

    private PlayMainItemAdapter mAdapter;
    private List<DownLoadModel> playDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playsearch);
        ButterKnife.bind(this);
    }

    @Override
    public void initWidget(View... v) {
        super.initWidget(v);
        hideTitleBar();

        editSearchDetail.setOnEditorActionListener(EnterListenter);

        mAdapter = new PlayMainItemAdapter(mContext, playDataList, this, null);
        playmainListview.setAdapter(mAdapter);
    }

    @OnClick({R.id.bar_back_iv, R.id.bar_back_ll, R.id.iv_search_detail})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back_iv:
            case R.id.bar_back_ll:
                finish();
                break;
            case R.id.iv_search_detail:
                checkdata();
                break;
        }
    }

    private void checkdata() {
        String temp = editSearchDetail.getText().toString().trim();
        if (temp!=null && !temp.isEmpty()) {
            showLoading(getResources().getString(R.string.toast_checking_str));
            checkDBDownloadData(temp);
        } else {
            showToast(getResources().getString(R.string.toast_search_empty_str));
        }
    }

    private TextView.OnEditorActionListener EnterListenter = new TextView.OnEditorActionListener() {
        /**
         * 参数说明
         * @param v 被监听的对象
         * @param actionId  动作标识符,如果值等于EditorInfo.IME_NULL，则回车键被按下。
         * @param event    如果由输入键触发，这是事件；否则，这是空的(比如非输入键触发是空的)。
         * @return 返回你的动作
         */
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                checkdata();
            }
            return false;
        }
    };

    private void checkDBDownloadData(String str) {
        if (mAdapter!=null) {
            playDataList.clear();
            List<DownLoadModel> tempList = DownloadManager.getInstance().searchDownloadByKey(str);
            if (tempList!=null && tempList.size()>0) {
                playDataList.addAll(tempList);
                mAdapter.notifyDataSetChanged();
            } else {
                showToast(getResources().getString(R.string.getnetdata_nomore));
            }
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editSearchDetail.getWindowToken(), 0);
            cancelLoading();
        }
    }

    @Override
    public void onAdapterItemClick(View view, int position) {
        DownLoadModel start = mAdapter.getItem(position);
        switch (start.getStatus()) {
            case CommonDownloadTask.STATUS_FINISH:
                StartGame(start.getDownloadUrl(), start.getGamePath()+"/"+start.getGameZipName().replace(".zip",""));
                break;
        }
    }
}
